package com.kaishengit.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kaishengit.dao.UserDao;
import com.kaishengit.entity.User;
import com.kaishengit.util.DateUtil;

public class LoginServlet extends HttpServlet {

	
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if(username.equals("") || password.equals("")){
			response.sendRedirect("index.jsp?code=1001");
			return;
		}
		User user = new User();
		UserDao udao = new UserDao();
		user = udao.findByUsername(username);
		if(user!=null){
			if(user.getPassword().equals(password)){
				String time = DateUtil.getNow();
				user.setLoginTime(time);
				udao.updateLoginTime(user);
				System.out.println(user.getLoginTime());
				HttpSession session = request.getSession();
				session.setAttribute("user",user );
				response.sendRedirect("main.jspx");
			}	else{
				response.sendRedirect("index.jsp?code=1001");
			}
			
		}	else{
			response.sendRedirect("index.jsp?code=1001");
		}
		

		
	}

}
